/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.vttu.ui;

import java.awt.BorderLayout;
import java.sql.Connection;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import vn.edu.vttu.data.ColoredTableCellRenderer;
import vn.edu.vttu.data.ConnectDB;
import vn.edu.vttu.data.RawMaterial;
import vn.edu.vttu.data.TableReservation;
import vn.edu.vttu.data.Unit;

/**
 *
 * @author nhphuoc
 */
public class PanelPrintListRawMaterial extends javax.swing.JPanel {

    /**
     * Creates new form PanelPrintListRawMaterial
     */
    JTable table;

    public PanelPrintListRawMaterial() {
        initComponents();
        pn.removeAll();
        printListRawmetarial();
        pn.updateUI();
        pn.repaint();
    }

    private String dvt(int id, int idunit, float num) {
        String kq="";
        
        return kq;
    }

    private void printListRawmetarial() {

        TableModel tb = TableReservation.getListRecipes(ConnectDB.conn());
        int n = 1;
        String row = "";
        for (int i = 0; i < tb.getRowCount(); i++) {
            String nameRecipes = tb.getValueAt(i, 0).toString();
            int idUnit = Integer.parseInt(tb.getValueAt(i, 1).toString());
            float number = Float.parseFloat(tb.getValueAt(i, 2).toString());
            int idRawmaterial = Integer.parseInt(tb.getValueAt(i, 3).toString());
            System.out.println(idRawmaterial);
            String x = dvt(idRawmaterial, idUnit, number);
            row = row  + (n + "," + tb.getValueAt(i, 0).toString() + "," + x)+"_";
            n++;            
        }
        //System.out.println(row.trim());        
        String[] r = row.split("_");
        Vector<Vector> rowData = new Vector<Vector>();
        for (int i = 0; i < r.length; i++) {
            Vector<String> rowOne = new Vector<String>();
            System.out.println(r[i].toString());
            String[] rr = r[i].split(",");
            for (int j = 0; j < rr.length; j++) {
                rowOne.addElement(rr[j].toString());
            }
            rowData.addElement(rowOne);
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("STT");
        columnNames.addElement("Tên Nguyên Liệu");
        columnNames.addElement("Số Lượng");
        table = new JTable(rowData, columnNames);        
        table.setGridColor(new java.awt.Color(204, 204, 204));
        table.setRowHeight(25);
        table.setSelectionBackground(new java.awt.Color(255, 153, 0));
        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        table.setFont(new java.awt.Font("Tahoma", 1, 10));
        //table.getColumnModel().getColumn(0).setMaxWidth(30);
        //table.getColumnModel().getColumn(1).setMaxWidth(150);
        //table.getColumnModel().getColumn(2).setMinWidth(140);
        JScrollPane scrollPane = new JScrollPane(table);
        pn.add(scrollPane, BorderLayout.CENTER);
        pn.setSize(300, 150);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pn = new javax.swing.JPanel();
        btnPrint = new javax.swing.JButton();
        btnReload = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        pn.setLayout(new java.awt.GridLayout());

        btnPrint.setText("Print");
        btnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintActionPerformed(evt);
            }
        });

        btnReload.setText("Reload");
        btnReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReloadActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("DANH SÁCH NGUYÊN LIỆU ĐẶT BÀN");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(btnPrint)
                .addGap(57, 57, 57)
                .addComponent(btnReload)
                .addContainerGap(119, Short.MAX_VALUE))
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pn, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPrint)
                    .addComponent(btnReload)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed
        JTable.PrintMode mode = JTable.PrintMode.FIT_WIDTH;
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        MessageFormat header = new MessageFormat("DANH SÁCH NGUYÊN LIỆU ĐẶT BÀN");
        MessageFormat footer = new MessageFormat("Ngày: " + formatter.format(new Date()));
        try {
            boolean comp = table.print(mode, header, footer, true, null, true, null);
            if (comp) {
                JOptionPane.showMessageDialog(getRootPane(), "Đã in");
            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_btnPrintActionPerformed

    private void btnReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReloadActionPerformed
        pn.removeAll();
        printListRawmetarial();
        pn.updateUI();
        pn.repaint();
    }//GEN-LAST:event_btnReloadActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btnReload;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel pn;
    // End of variables declaration//GEN-END:variables
}
